package net.myspring.rabbit.send;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.support.CorrelationData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.UUID;

/**
 * @author wangzm
 */
@RestController
public class SendController {

    @Autowired
    private AmqpTemplate rabbitTemplate;

    public void send() {
        String context = "hello " + new Date();
        System.out.println("Sender : " + context);
        this.rabbitTemplate.convertAndSend("hello", context);
    }

    public void fanoutSend() {
        String context = "fanoutSend " + new Date();
        System.out.println("fanoutSend : " + context);
        this.rabbitTemplate.convertAndSend("fanoutExchange","123",context);
    }

    public void topicMessageSend() {
        String context = "topicExchange " + new Date();
        System.out.println("topicExchange : " + context);
        this.rabbitTemplate.convertAndSend("topicExchange","topic.message",context);
    }

    public void topicMessagesSend() {
        String context = "topicMessagesSend " + new Date();
        System.out.println("topicMessagesSend : " + context);
        this.rabbitTemplate.convertAndSend("topicExchange","topic.message",context);
    }

    public void deadMessagesSend() {
//        声明消息处理器  这个对消息进行处理  可以设置一些参数   对消息进行一些定制化处理   我们这里  来设置消息的编码  以及消息的过期时间  因为在.net 以及其他版本过期时间不一致   这里的时间毫秒值 为字符串
        MessagePostProcessor messagePostProcessor = message -> {
            MessageProperties messageProperties = message.getMessageProperties();
//            设置编码
            messageProperties.setContentEncoding("utf-8");
//            设置过期时间10*1000毫秒
            messageProperties.setExpiration("5000");
            return message;
        };
//         向DL_QUEUE 发送消息  10*1000毫秒后过期 形成死信
        String context = "topicMessagesSend " + new Date();
        System.out.println("topicMessagesSend : " + context);
        rabbitTemplate.convertAndSend("DL_EXCHANGE", "DL_KEY",context, messagePostProcessor);
    }

}
